package com.fcmeditech.AsianSexyGirls.File;

import java.util.ArrayList;
import java.util.List;

import com.fcmeditech.AsianSexyGirls.CategaryObject;
import com.fcmeditech.AsianSexyGirls.R;


import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ListAdapter extends BaseAdapter {
    
    private Context context;
    public ImageDownloader load=new ImageDownloader(); 
    private ArrayList<CategaryObject> listsexys;
    public ImageLoader load2;
    BitmapManager2 bitmapManager2;

    public ListAdapter(Context c, ArrayList<CategaryObject> list) {
        context	 = c;
        listsexys = list;
        load2=new ImageLoader(c);
        bitmapManager2=new BitmapManager2(1);
		bitmapManager2.setPlaceholder(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher));
    }
    public int getCount() {
        return listsexys.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }
    
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        CategaryObject myGirl= listsexys.get(position);
        RowHolder holder = null;
        if(convertView==null) {
        	LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            vi = inflater.inflate(R.layout.listgirl, parent, false);  //insert layout of ROW
            holder = new RowHolder();
            holder.imgAvt = (ImageView)vi.findViewById(R.id.imgAvt);
            holder.txtTitle = (TextView)vi.findViewById(R.id.tvTitle);
            holder.txtDate  = (TextView)vi.findViewById(R.id.tvDateUpdate);
            vi.setTag(holder);

        }
        else
        {
    		
            holder = (RowHolder)vi.getTag();
        }
        
        holder.txtTitle.setText(myGirl.getCateName());
        holder.txtDate.setText(myGirl.getDateUpdate());
        
        ///load image from link -->avt in listview
        String link=myGirl.getUrl_image();
        
        //*********Method 1: no other thread, simple decode **********************************
        //Bitmap result=load.simpleDownloadBitmap(link); // There are sometimes lossing images!
        //holder.imgAvt.setImageBitmap(result);
        
        //*********Method 2: no other thread, fix bug in Method 1 ****************************
        //Bitmap result=load.fineDownloadBitmap(link); 
        //holder.imgAvt.setImageBitmap(result);
        
        //*********Method 3: async thread, load images in the order, *************************
        // not save images so when scroll LISTVIEW, we again download and display in Avatar !!!
        // load.download(link, holder.imgAvt);
        
        //*********Method 4: old, load images in other thread and save images in Memorycache
        //load2.DisplayImage(link, holder.imgAvt);
        //*********Method 5: using Executor to download images
        bitmapManager2.loadBitmap(link, holder.imgAvt, holder.imgAvt.getWidth(),holder.imgAvt.getHeight(),position); //--->load bitmap from Cache 

        
        return vi;
    }
    static class RowHolder
	{
		ImageView imgAvt;
		TextView txtTitle;
		TextView txtDate;
	}
}